package com.shop2cn.iapi.agentproducts.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.BatchSetOnshelfBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.service.BatchSetOnshelfCall;
import com.ymatou.iapi.godmch.parameter.setShareTypeBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
 * 批量上架 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchSetOnshelf {
	private static BatchSetOnshelfBean batchsetonshelfBean;
	private static BatchSetOnshelfCall batchsetonshelfCall;
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// 渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架");
	}

	@Before
	public void caseUp() {
		batchsetonshelfBean = new BatchSetOnshelfBean();
		batchsetonshelfCall = new BatchSetOnshelfCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchsetonshelfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchSetOnshelf_001() {
		Logger.start(true, "下架商品批量上架，成功");
		try {
			//新建两个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductBean addAgentProductBean1 = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId1 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean1);
			//下架
			AddAgentProductCallService.SetOffshelf(productId, rootUserId);
			AddAgentProductCallService.SetOffshelf(productId1, rootUserId);
			//批量上架
			batchsetonshelfBean.setUserId(rootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList(productId,productId1));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_002() {
		Logger.start(true, "部分商品无货，成功");
		try {
			//新建两个下架商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			addAgentProductBean.setSaleType(2);
			addAgentProductBean.getCatalogList().get(0).setStock(0);
			addAgentProductBean.getCatalogList().get(1).setStock(0);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductBean addAgentProductBean1 = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			addAgentProductBean1.setSaleType(2);
			String productId1 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean1);
			//批量上架
			batchsetonshelfBean.setUserId(rootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList(productId,productId1));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
			} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_003() {
		Logger.start(true, "有商品已经上架，上架成功");
		try {
			//新建两个下架商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			addAgentProductBean.setSaleType(2);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductBean addAgentProductBean1 = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId1 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean1);
			//批量上架
			batchsetonshelfBean.setUserId(rootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList(productId,productId1));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	


	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_005() {
		Logger.start(true, "非渠道商上架商品,无权限，上架失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,rootUserId);
			batchsetonshelfBean.setUserId(agentUserId1);
			batchsetonshelfBean.setProductIdList(Arrays.asList(agentProductId));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
			JsonObject productMsgList = batchsetonshelfCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals(agentProductId, productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("代理商品不可操作上下架", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_006() {
		Logger.start(true, "无效商品id，上架失败");
		try {
			//批量上架
			batchsetonshelfBean.setUserId(rootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList("123"));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
			JsonObject productMsgList = batchsetonshelfCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals("123", productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("商品已被删除", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_007() {
		Logger.start(true, "上架一个价格风控商品，上架失败");
		try {
			rootUserId= GodmchManageCallService.CreateSellerMch();
			//修改限制为20000
			setShareTypeBean initsetShareTypeV2 = GodmchManageCallService.initsetShareTypeV2(rootUserId);
			GodmchManageCallService.setShareTypeV2Call(initsetShareTypeV2);
			Thread.sleep(2000);
			//发布一个超过一万的下架商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			addAgentProductBean.setSaleType(2);
			addAgentProductBean.setCurrency("CNY");
			addAgentProductBean.getCatalogList().get(0).setDirectPrice(10001.00);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//修改限制为10000
			initsetShareTypeV2 = GodmchManageCallService.initsetShareTypeV2(rootUserId);
			initsetShareTypeV2.setProductRiskAmount(new BigDecimal(10000));
			GodmchManageCallService.setShareTypeV2Call(initsetShareTypeV2);
			//上架失败
			batchsetonshelfBean.setUserId(rootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList(productId));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("400", batchsetonshelfCall.getString("code"), "验证code");
			Logger.verifyIsContains("单价超过限额", batchsetonshelfCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	public void Tc_BatchSetOnshelf_901() {
		Logger.start(true, "业务测试批量上架");
		try {
			//批量上架
			TestDataManager.getData("up").stream().forEach(pid->{
				try {
					batchsetonshelfBean.setUserId(500587194);
					batchsetonshelfBean.setProductIdList(Arrays.asList(pid));
					batchsetonshelfCall.setData(batchsetonshelfBean);
					batchsetonshelfCall.callService();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			});;
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchSetOnshelf_008() {
		Logger.start(true, "上架，自动铺货");
		try {
			int vipRootUserId=500781768;
			int vipAgentUserId=500781769;
			int vipAgentUserId1=500781770;

			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					"",true);
			addAgentProductBean.setTitle("继承"+TestDataManager.getRandomNum(6));
			addAgentProductBean.setSaleType(2);
			String addAgentProduct = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(vipAgentUserId);
			Logger.verifyNotEquals(addAgentProductBean.getTitle(), getProductLists.get(0).getTitle(),"验证标题");
			
			List<ProductListDto> agentLists = AddAgentProductCallService.GetProductLists(vipAgentUserId1);
			Logger.verifyNotEquals(addAgentProductBean.getTitle(), agentLists.get(0).getTitle(),"验证标题");
			
			//批量上架
			batchsetonshelfBean.setUserId(vipRootUserId);
			batchsetonshelfBean.setProductIdList(Arrays.asList(addAgentProduct));
			batchsetonshelfCall.setData(batchsetonshelfBean);
			batchsetonshelfCall.callService();
			Logger.verifyEquals("200", batchsetonshelfCall.getString("code"), "验证code");
			Thread.sleep(5000);
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(vipAgentUserId);
			Logger.verifyEquals(addAgentProductBean.getTitle(), getProductLists1.get(0).getTitle(),"验证标题");
			
			List<ProductListDto> agentLists1 = AddAgentProductCallService.GetProductLists(vipAgentUserId1);
			Logger.verifyEquals(addAgentProductBean.getTitle(), agentLists1.get(0).getTitle(),"验证标题");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private void AddAgentProduct(AddAgentProductBean addAgentProductBean) {
		// TODO Auto-generated method stub
		
	}
	
	
}